<?php
namespace Common\Model;
use Think\Model;

/**
 * Class CsdtWatereleRecordModel
 * @package Common\Model
 * 调蓄池水电表运行记录表
 */
class CsdtWatereleRecordModel extends BaseModel{
    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'date desc';

    public function get_list_use_page($map=array(), $cur_page, $rows_num){
        if(isset($map['eleid'])){
            $map['eleid'][0] = 'eq';
            $map['eleid'][1] = str_replace('%', '', $map['eleid'][1]);
        }
        if($map['start']&& $map['end']){
            $map['start'][1] = str_replace('%', '', $map['start'][1]);
            $map['end'][1] = str_replace('%', '', $map['end'][1]);
            $date_str = '(date>=\''.$map['start'][1].' 00:00:00'.'\' and date<=\''.$map['end'][1].' 23:59:59'.'\') ';
            unset($map['start']);
            unset($map['end']);
        }
        $station_id = str_replace('%', '', $map['ps_id'][1]);
        $station_id = array_filter(explode(',', $station_id));
        $temp_ids = array();
        $where_str = '';
        $map = filter_query_map($map);
        $station_model = D('Csdt');
        $faly_model = D('CsdtWaterele');
        $map = filter_query_map($map);
        $where_str = '';
        for($j=0;$j<count($station_id);$j++){
            $pumps = $faly_model->queryField('ps_id', $station_id[$j]);
            for($i=0;$i<count($pumps);$i++){
                $temp_ids[] = $pumps[$i]['id'];
            }
        }
        for($i=0;$i<count($temp_ids);$i++){
            $where_str .= 'eleid='.$temp_ids[$i];
            if($i < count($temp_ids)-1){
                $where_str .= ' or ';
            }
        }
        if($date_str&&$where_str)
            $where_str = $date_str.' and ('.$where_str.')';
        else if($date_str&&!$where_str)
            $where_str = $date_str;
        unset($map['ps_id']);
        if($where_str){
            $count = $this->where($map)->where($where_str)->count();
            $list = $this->where($map)->where($where_str)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        }else if(!$where_str&&!empty($map)){
            $count = $this->where($map)->count();
            $list = $this->where($map)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        }else if(empty($station_id)){
            $count = $this->count();
            $list = $this->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        }else if(!empty($station_id)){
            $count = 0;
            $list = array();
        }
        unset($map['ps_id']);
        foreach($list as $k => $v){
            $temp_pump = ($faly_model->queryObj($v['eleid'], 'id'));
            $sta_temp = $station_model->queryObj($temp_pump['ps_id'], 'id');
            $list[$k]['sta_name'] = $sta_temp['ps_name'];
            $list[$k]['we_number'] = $temp_pump['we_number'];
            $list[$k]['we_name'] = $temp_pump['we_name'];
        }

        return array('total'=>$count, 'list'=>$list);
    }

    public function filter_all($pid){
        $admin_model = D('MobileAdmin');
        $station_model = D('Csdt');
        $pump_model = D('CsdtWaterele');
        $fa = $pump_model->queryObj($pid, 'id');
        $staion = $station_model->queryObj($fa['ps_id'], 'id');
        $list = $this->queryField('eleid', $pid);
        foreach($list as $k => $v){
            $admin = $admin_model->queryObj($list[$k]['opr'], 'id');
            $list[$k]['opr'] = $admin['realname'];
            $list[$k]['station'] = $staion['ps_name'];
            $list[$k]['eleid'] = $fa['we_number'];
        }
        return $list;
    }

    public function get_last_one($pid){
        $map['eleid'] = array('eq', $pid);
        $one = $this->where($map)->order('date desc')->find();
        return $one?$one['status']:999;
    }

    public function get_last_one_record($pid){
        $map['eleid'] = array('eq', $pid);
        $one = $this->where($map)->order('date desc')->find();
        return $one?$one:false;
    }

    public function addObj_mobile($post, $f_title='id'){
        //判重
        $data[$f_title] = array("eq", $post[$f_title]);
        $one = $this->where($data)->find();
        $post['date'] = getNowTime();
        switch($post['status']){
            case 0:
                break;
            case 1:
                break;
        }
        if(!$one){
            //如果没有记录，就添加
            return $this->add($post);
        }else{
            //反之更新
            return $this->save($post);
        }
    }

    public function bz_record($fy, $list){
        foreach($list as $k => $item){
            $list[$k]['date'] = substr($list[$k]['date'],0,10);
            $tmx = D('MobileAdmin')->queryObj($list[$k]['opr'], 'id');
            $list[$k]['opr'] = $tmx['realname'];
        }
        return $list;
    }
}